package com.spa.modules.system.task;

import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import com.spa.common.utils.RedisUtil;
import com.spa.modules.system.entity.SpaUser;
import com.spa.modules.system.mapper.SpaUserMapper;
import com.spa.modules.system.service.SpaUserRoleService;

/**
 * 项目启动初始话redis 数据
 * 
 * @author Administrator
 *
 */
//@Component
public class InitTask implements ServletContextAware {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SpaUserMapper spaUserMapper;
    
    @Autowired
    private SpaUserRoleService spaUserRoleService;

    /**
     * 在填充普通bean属性之后但在初始化之前调用
     * 类似于initializingbean的afterpropertiesset或自定义init方法的回调
     *
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        System.out.println("setServletContext方法");

        setUserListToRedis();
        
        spaUserRoleService.setPermissionListToRedis();
    }

    /**
     * 将用户角色信息
     */
    private void setUserListToRedis() {
        List<SpaUser> spaUserList = spaUserMapper.selectList(null);
        for (SpaUser spaUser : spaUserList) {
            redisUtil.set("userRole:" + spaUser.getId(), spaUser.getRoleId());
        }
    }
}
